from DrissionPage import ChromiumPage, ChromiumOptions
import time
import pandas as pd
from lxml import etree
import os  # 添加os模块用于检查文件是否存在

options = ChromiumOptions()
options.set_local_port(9222)  # 设置调试端口
page = ChromiumPage(addr_or_opts=options)

tab = page.latest_tab

tab.get('https://www.qcc.com/cassets/890736a6a7f3d76db4e9172f96b2f5e4.html')

time.sleep()


def get_html():
    zhishi_html = tab.html
    tree = etree.HTML(zhishi_html)
    return tree


def get_zhuanlilist(zhuanlilist, name_h1):
    items = []
    for i in zhuanlilist:
        item_zhuanli = []
        patent_name = i.xpath('./td[2]/a/text()')[0]
        Patent_type = i.xpath('./td[3]/text()')[0]
        legal_status = i.xpath('./td[4]/span/text()')[0]
        app_number = i.xpath('./td[5]/text()')[0]
        aply_date = i.xpath('./td[6]/text()')[0]
        Publication_Number = i.xpath('./td[7]//span/text()')[0]
        Public_Date = i.xpath('./td[8]/text()')[0]
        inventor = i.xpath('./td[9]/text()')[0]
        item_zhuanli.extend([name_h1, patent_name, Patent_type, legal_status,
                            app_number, aply_date, Publication_Number, Public_Date, inventor])
        items.append(item_zhuanli)
    return items


def get_data(items):
    file_exists = os.path.exists('zhuanli.csv')
    df = pd.DataFrame(items, columns=['公司名称', '发明名称', '专利类型', '法律状态', '申请号', '申请日期',
                                      '公开号', '公开日期', '发明人'])
    df.to_csv('zhuanli.csv', mode='a',
              index=False, header=not file_exists)
    if not file_exists:
        print("创建新文件并写入表头和数据")
    else:
        print("数据已追加到 zhuanli.csv")


def get_ruanjianlist(ruanjianlist, name_h1):
    items = []
    for i in ruanjianlist:
        item_ruanjian = []
        patent_name = i.xpath('./td[2]/text()')[0]
        Patent_type = i.xpath('./td[3]/text()')[0]
        legal_status = i.xpath('./td[4]/text()')[0]
        app_number = i.xpath('./td[5]/text()')[0]
        aply_date = i.xpath('./td[6]/text()')[0]
        Publication_Number = i.xpath('./td[7]//text()')[0]
        Public_Date = i.xpath('./td[8]/text()')[0]
        inventor = i.xpath('./td[9]/text()')[0]
        item_ruanjian.extend([name_h1, patent_name, Patent_type, legal_status,
                              app_number, aply_date, Publication_Number, Public_Date, inventor])
        items.append(item_ruanjian)
    return items


def get_data1(items):
    file_exists = os.path.exists('ruanjian.csv')
    df = pd.DataFrame(items, columns=['公司名称', '软件全称', '软件简称', '版本号', '登记号', '开发完成日期',
                                      '首次发布日期', '登记日期', '权力取得方式'])
    df.to_csv('ruanjian.csv', mode='a',
              index=False, header=not file_exists)
    if not file_exists:
        print("创建新文件并写入表头和数据")
    else:
        print("数据已追加到 ruanjian.csv")


# 获取企业名称
tree1 = get_html()
name_h1 = tree1.xpath('//h1/text()')[0]


try:
    tree1 = get_html()
    zhuanlilist = tree1.xpath(
        '//div[@class="data-assets"]/div[@class="company-data"]/section[@id="zhuanlilist"]/div[@class="track-table-part"]//tr[position() > 1]')
    if zhuanlilist[0]:
        pass
    while True:
        zhaunli_list = get_zhuanlilist(zhuanlilist, name_h1)
        get_data(zhaunli_list)
        # print(zhaunli_list)
        try:
            # 等待翻页按钮加载完成
            tab.wait.ele_displayed(
                'xpath://div[@class="data-assets"]/div[@class="company-data"]/section[@id="zhuanlilist"]//ul[@class="pagination"]/li[last()]/a', timeout=10)
            # 获取翻页按钮
            next_page = tab.ele(
                'xpath://div[@class="data-assets"]/div[@class="company-data"]/section[@id="zhuanlilist"]//ul[@class="pagination"]/li[last()]/a')
            # 检查翻页按钮是否可点击
            next_number = tree1.xpath(
                '//div[@class="data-assets"]/div[@class="company-data"]/section[@id="zhuanlilist"]//ul[@class="pagination"]/li[last()]/a/text()')[0]
            print(next_number)
            if next_number != '>':
                break
            next_page.click()
            # 等待页面加载完成
            tab.wait.load_start()
            # 重新获取页面 HTML
            zhishi_html = tab.html
            tree1 = etree.HTML(zhishi_html)
            zhuanlilist = tree1.xpath(
                '//div[@class="data-assets"]/div[@class="company-data"]/section[@id="zhuanlilist"]/div[@class="track-table-part"]//tr[position() > 1]')
        except Exception as e:
            print(f"翻页失败: {e}")
            break
except:
    print('没有专利信息')

try:
    tree1 = get_html()
    ruanjianlist = tree1.xpath(
        '//div[@class="data-assets"]/div[@class="company-data"]/section[@id="rjzzqlist"]//tr[position() > 1]')
    if ruanjianlist[0]:
        pass
    while True:
        ruanjian_list = get_ruanjianlist(ruanjianlist, name_h1)
        # print(ruanjian_list)
        get_data1(ruanjian_list)
        try:
            # 等待翻页按钮加载完成
            tab.wait.ele_displayed(
                'xpath://div[@class="data-assets"]/div[@class="company-data"]/section[@id="rjzzqlist"]//ul[@class="pagination"]/li[last()]/a', timeout=10)
            # 获取翻页按钮
            next_page = tab.ele(
                'xpath://div[@class="data-assets"]/div[@class="company-data"]/section[@id="rjzzqlist"]//ul[@class="pagination"]/li[last()]/a')
            # 检查翻页按钮是否可点击
            next_number = tree1.xpath(
                '//div[@class="data-assets"]/div[@class="company-data"]/section[@id="rjzzqlist"]//ul[@class="pagination"]/li[last()]/a/text()')[0]
            # print(next_number)
            if next_number != '>':
                break
            next_page.click()
            # 等待页面加载完成
            tab.wait.load_start()
            # 重新获取页面 HTML
            ruanjian_html = tab.html
            tree1 = etree.HTML(ruanjian_html)
            ruanjianlist = tree1.xpath(
                '//div[@class="data-assets"]/div[@class="company-data"]/section[@id="rjzzqlist"]//tr[position() > 1]')
        except Exception as e:
            print(f"翻页失败: {e}")
            break
except:
    print(f'{name_h1}没用软件著作权！！！！！！！')
